package com.frx.fakeroutexposed.fragments;


import android.app.DialogFragment;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.frx.fakeroutexposed.R;
import com.frx.fakeroutexposed.events.ConfigEvent;
import com.frx.fakeroutexposed.tools.MY_NAMES;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;



public class ListRouteFragment extends DialogFragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.list_route_fragment, container);
		//去掉对话框的标题
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		///==========================================
		//填充ListView
		final ListView mFileListView =(ListView) view.findViewById(R.id.listView_route);
		mFileListView.setAdapter(new ArrayAdapter<String>
				(getActivity()
						, android.R.layout.simple_list_item_1
						, getFileNameNoEx()));
		//==========================================
		//==========================================
		//添加点击事件
		mFileListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {

				//取得点击的文件名的完整路径
				String mFileName = Environment.getExternalStorageDirectory().getPath()
						+MY_NAMES.DIR_NAME_GPS_route+(String)mFileListView.getItemAtPosition(position)
						+MY_NAMES.FILE_NAME_EX;
				Log.d("文件的完整路径:", mFileName);
				//EventBus post
				ConfigEvent event =new ConfigEvent();
				// 接收者在 BackgroundsServices 类的 onEventConfig2SharedPreferences()
				// 将完整的路径名,保存在 SharedPreferences 中
				event.setGpsDataFileNameFullPath(mFileName);

				//接收者在 MapViewFragments 类的 onEventDisplayFileName()
				//用于将文件名显示在界面上
				event.setGpsDataFileNOEXName((String)mFileListView.getItemAtPosition(position));

				EventBus.getDefault().post(event);
				//end EventBus post
				//关闭该 fragment
				dismiss();
			}

		});//end 单击事件
		//==========================================
		return view;
	}//end onCreateView()
	//==============================================

	/**获取轨迹文件的名字,不包括扩展名
	 * @return 文件名(不包括扩展名)的List
	 */
	private List<String> getFileNameNoEx(){
		List<String> fileNameList =new ArrayList<String>();
		try {
			//1.判断是否存在 sdcard
			if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
				//目录
				File path = new File(Environment.getExternalStorageDirectory().getPath()
						+MY_NAMES.DIR_NAME_GPS_route);
				if(!path.exists()){
					//2.创建目录，
					path.mkdir();
				}
				//如果是文件夹的话
				if(path.isDirectory()){
					File[] files = path.listFiles();
					//先判断下有没有权限，如果没有权限的话，就不执行了
					if(null == files){
						return null;
					}
					//===================================
					//取出扩展名为.route的文件名
					for (int i = 0; i < files.length; i++) {
						String string = files[i].getName();
						if (string.endsWith(MY_NAMES.FILE_NAME_EX)) {
							fileNameList.add(string.substring(0, string.lastIndexOf(".")));
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return fileNameList;
	}
	//=============================================

}
